package com.fieldchain.ex.fa100.utils.string;

import java.math.BigDecimal;

public class NumberFormate {
    public static String m2(double f) {
        return m(f, 2);
    }

    public static String m2(String str) {
        return m(str, 2);
    }

    public static String m3(double f) {
        return m(f, 3);
    }

    public static String m3(String str) {
        return m(str, 3);
    }

    public static String m5(double f) {
        return m(f, 5);
    }

    public static String m5(String str) {
        return m(str, 5);
    }


    public static String m(double f, int num) {
        return m(f + "", num);
    }

    public static String m(String str, int num) {
        BigDecimal decimal = new BigDecimal(str);
        str = decimal.toPlainString();
        String left = "";
        String right = "";
        String[] aar = str.split("\\.");
        StringBuffer buffer = new StringBuffer();
        if (aar.length == 1) {
            left = str;
        } else if (aar.length == 2) {
            left = aar[0];
            right = aar[1];
        }
        if (aar.length > 1) {
            if (right.length() > num) {
                right = right.substring(0, num);
            } else {
                int len = num - right.length();
                for (int i = 0; i < len; i++) {
                    right += "0";
                }
            }
        } else {
            for (int i = 0; i < num; i++) {
                right += "0";
            }
        }

        if (left.length() > 3) {
            StringBuffer leftBuffer = new StringBuffer();
            left = new StringBuffer(left).reverse().toString();
            char[] leftAar = left.toCharArray();
            for (int i = 0; i < leftAar.length; i++) {
                leftBuffer.append(leftAar[i]);
                if ((i + 1) % 3 == 0 && i < (leftAar.length - 1)) {
                    leftBuffer.append(",");
                }
            }
            left = leftBuffer.reverse().toString();
        }
        buffer.append(left).append(".").append(right);
        return buffer.toString();

    }
    public static String m(String str, int num,boolean tags) {
        BigDecimal decimal = new BigDecimal(str);
        str = decimal.toPlainString();
        String left = "";
        String right = "";
        String[] aar = str.split("\\.");
        StringBuffer buffer = new StringBuffer();
        if (aar.length == 1) {
            left = str;
        } else if (aar.length == 2) {
            left = aar[0];
            right = aar[1];
        }
        if (aar.length > 1) {
            if (right.length() > num) {
                right = right.substring(0, num);
            } else {
                int len = num - right.length();
                for (int i = 0; i < len; i++) {
                    right += "0";
                }
            }
        } else {
            for (int i = 0; i < num; i++) {
                right += "0";
            }
        }

        buffer.append(left).append(".").append(right);
        return buffer.toString();

    }

}
